﻿using Helper;
using HZY.Framework.DependencyInjection;
using Mapster;
using Model.Entity;
using Model.EntityInfo;

namespace DAL.Service
{
    public class RoleService : BaseService<RoleEntity>, IScopedSelfDependency 
    {
        /// <summary>
        /// 获取全部角色列表
        /// </summary>
        /// <returns></returns>
        public async Task<BaseResult<List<RoleInfoModel>>> getAllRole()
        {
            var list = await DB.SqlSugarClient.Queryable<RoleEntity>()
                .Includes(e => e.Routes).ToListAsync();
            return new BaseResult<List<RoleInfoModel>>()
            {
                Code = SystemEnums.ResCode.Success,
                Data = list.Adapt<List<RoleInfoModel>>(),
                Msg = ""
            };
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<BaseResult<string>> AddRoleAsync(RoleEntity role, List<int> routeIds)
        {


            try
            {
                DB.SqlSugarClient.Ado.BeginTran();
                var row = await DB.SqlSugarClient.Insertable<RoleEntity>(role).ExecuteReturnEntityAsync();

                if (row != null)
                {
                    List<RouteBtRoleEntity> entities = new List<RouteBtRoleEntity>();
                    foreach (var id in routeIds)
                    {
                        entities.Add(new RouteBtRoleEntity()
                        {
                            RoleId = row.Id,
                            RouteId = id
                        });
                    }
                    int res = await DB.SqlSugarClient.Insertable<RouteBtRoleEntity>(entities).ExecuteCommandAsync();
                    if (res > 0)
                    {

                        DB.SqlSugarClient.Ado.CommitTran();
                        return new BaseResult<string>()
                        {
                            Code = SystemEnums.ResCode.Success,
                            Data = "",
                            Msg = ""
                        };
                    }
                    else
                    {
                        DB.SqlSugarClient.Ado.RollbackTran();

                        return new BaseResult<string>()
                        {
                            Code = SystemEnums.ResCode.Success,
                            Data = "",
                            Msg = "新增失败"
                        };
                    }
                }
                else
                {
                    DB.SqlSugarClient.Ado.RollbackTran();

                    return new BaseResult<string>()
                    {
                        Code = SystemEnums.ResCode.Success,
                        Data = "",
                        Msg = "新增失败"
                    };
                }
            }
            catch (Exception ex)
            {
                DB.SqlSugarClient.Ado.RollbackTran();

                return new BaseResult<string>()
                {
                    Code = SystemEnums.ResCode.Fail,
                    Data = "",
                    Msg = ex.Message
                };
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BaseResult<string>> DellRoleAsync(int id)
        {
            var rows = await DB.SqlSugarClient.Deleteable<RoleEntity>(new RoleEntity() { Id = id }).ExecuteCommandAsync();
            if (rows > 0)
            {
                return new BaseResult<string>()
                {
                    Code = SystemEnums.ResCode.Success,
                    Data = "",
                    Msg = "删除成功"
                };
            }
            return new BaseResult<string>()
            {
                Code = SystemEnums.ResCode.Fail,
                Data = "",
                Msg = "删除失败"
            };
        }/// <summary>
         /// 更新角色
         /// </summary>
         /// <param name="role"></param>
         /// <returns></returns>
        public async Task<BaseResult<string>> UpdateRoleAsync(RoleEntity role, List<int> routeIds)
        {


            try
            {
                DB.SqlSugarClient.Ado.BeginTran();
                var row = await DB.SqlSugarClient.Updateable<RoleEntity>(role).ExecuteCommandAsync();

                if (row > 0)
                {
                    await DB.SqlSugarClient.Deleteable<RouteBtRoleEntity>().Where(it => it.RoleId == role.Id).ExecuteCommandAsync();
                    List<RouteBtRoleEntity> entities = new List<RouteBtRoleEntity>();
                    foreach (var id in routeIds)
                    {
                        entities.Add(new RouteBtRoleEntity()
                        {
                            RoleId = role.Id,
                            RouteId = id
                        });
                    }
                    int res = await DB.SqlSugarClient.Insertable<RouteBtRoleEntity>(entities).ExecuteCommandAsync();
                    if (res > 0)
                    {

                        DB.SqlSugarClient.Ado.CommitTran();
                        return new BaseResult<string>()
                        {
                            Code = SystemEnums.ResCode.Success,
                            Data = "",
                            Msg = "更新成功"
                        };
                    }
                    else
                    {
                        DB.SqlSugarClient.Ado.RollbackTran();

                        return new BaseResult<string>()
                        {
                            Code = SystemEnums.ResCode.Success,
                            Data = "",
                            Msg = "更新失败"
                        };
                    }
                }
                else
                {
                    DB.SqlSugarClient.Ado.RollbackTran();

                    return new BaseResult<string>()
                    {
                        Code = SystemEnums.ResCode.Success,
                        Data = "",
                        Msg = "更新失败"
                    };
                }
            }
            catch (Exception ex)
            {
                DB.SqlSugarClient.Ado.RollbackTran();

                return new BaseResult<string>()
                {
                    Code = SystemEnums.ResCode.Fail,
                    Data = "",
                    Msg = ex.Message
                };
            }
        }
    }
}
